package com.cw.jeeyt.controller.system;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.system.SystemFileTemplateBo;
import com.cw.jeeyt.service.intf.system.SystemFileTemplateService;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 模板配置信息控制层
 *
  * @author chenw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/systemFileTemplate")
public class SystemFileTemplateController extends BaseController {
    @Resource
    private SystemFileTemplateService systemFileTemplateService;//模板配置信息业务接口

    /**
     * 跳转到模板配置信息列表查询页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() throws Exception {
        ModelAndView mv = new ModelAndView("system/systemFileTemplate_index");
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listSystemFileTemplate")
    public ResultData listSystemFileTemplate(@RequestBody DataTable dataTable) throws Exception {

        SystemFileTemplateBo systemFileTemplateBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            SystemFileTemplateBo.class);
        if (systemFileTemplateBo == null) {
            systemFileTemplateBo = new SystemFileTemplateBo();
        }
        PageInfo<SystemFileTemplateBo> pageInfo = systemFileTemplateService.queryByPage(systemFileTemplateBo,
            dataTable.getStart(), dataTable.getLength());

        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到模板配置信息表单页面
     *
     * @param id 主键
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView editForm(String id, String operation) throws Exception {

        ModelAndView mv = new ModelAndView("system/systemFileTemplate_form");
        SystemFileTemplateBo systemFileTemplateBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            systemFileTemplateBo = new SystemFileTemplateBo();
        } else {
            systemFileTemplateBo = systemFileTemplateService.getSystemFileTemplateById(id);
        }
        mv.addObject("systemFileTemplate", systemFileTemplateBo);
        mv.addObject("operation", operation);
        return mv;
    }

    /**
     * 根据操作类型保存模板配置信息数据
     *
     * @param systemFileTemplateBo
     * @param operation add 新增  edit 修改
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/saveSystemFileTemplate", method = RequestMethod.POST)
    public JsonResultDto saveSystemFileTemplate(SystemFileTemplateBo systemFileTemplateBo, String operation) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                systemFileTemplateService.insert(systemFileTemplateBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                systemFileTemplateService.update(systemFileTemplateBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            jsonResultDto.setMsg("保存失败:" + e.getMessage());
            e.printStackTrace();
            logger.error("保存模板配置信息失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 删除模板配置信息
     *
     * @param systemFileTemplateBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto delete(SystemFileTemplateBo systemFileTemplateBo) throws Exception {
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemFileTemplateService.delete(systemFileTemplateBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("删除失败：" + e.getMessage());
            logger.error("删除模板配置信息失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
}
